<template>
  <div class="specificationConfig-page">
    <div class="search-field">
      <el-form :model="searchForm" inline>
        <el-form-item label="编码" prop="number">
          <el-input
            placeholder="请输入编码"
            v-model="searchForm.number"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input
            placeholder="请输入名称"
            v-model="searchForm.name"
            autocomplete="off"
          />
        </el-form-item>
        <el-form-item label="状态" prop="enabled">
          <el-select
            style="width: 240px"
            v-model="searchForm.enabled"
            placeholder="选择状态"
          >
            <el-option
              v-for="option in statusOptions"
              :key="option.id"
              :label="option.name"
              :value="option.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <div>
            <el-button type="primary" @click="startSearch"> 查询 </el-button>
            <el-button @click="startClear"> 重置 </el-button>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div class="table-pagination-content">
      <div class="btns-fun-field">
        <!-- v-if="userPermission.addUserPerm" -->
        <el-button
          type="primary"
          @click.prevent="showAddEditDialog"
          :icon="Plus"
        >
          新增规格配置
        </el-button>
      </div>
      <div class="table-field">
        <el-table
          :data="tableData"
          border
          stripe
          style="width: 100%"
          v-loading="tableLoading"
          row-key="id"
        >
          <el-table-column prop="name" label="名称" width="190" />
          <el-table-column prop="number" label="编码" width="190" />
          <el-table-column prop="enabled" label="状态" width="190">
            <template #default="scope">
              <el-tag
                :type="scope.row.enabled === 1 ? 'danger' : 'success'"
                disable-transitions
              >
                {{ scope.row.enabled === 1 ? '禁用' : '启用' }}
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" width="210" />
          <el-table-column label="操作" min-width="220px" fixed="right">
            <template #default="scope">
              <!-- v-if="rolePermission.editRolePerm" -->

              <el-button
                type="success"
                size="small"
                @click.prevent="showAddEditDialog(scope.row, 2)"
                >编辑</el-button
              >
              <!-- v-if="rolePermission.removeRolePerm" -->
              <el-button
                type="danger"
                size="small"
                @click.prevent="showRemoveDialog(scope.row)"
                >删除</el-button
              >
              <el-button
                v-if="!scope.row.specNumber"
                type="primary"
                size="small"
                @click.prevent="showAddSubItemDialog(scope.row, 2)"
                >增加规格子项</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pagination-field">
        <el-pagination
          v-model:current-page="formData.pageIndex"
          v-model:page-size="formData.pageSize"
          :page-sizes="[10, 20, 30, 40]"
          :background="true"
          layout="total, sizes, prev, pager, next, jumper"
          :total="formData.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <SpecificationConfigAddUpdate
      v-model="addUpdateRef"
    ></SpecificationConfigAddUpdate>
    <SpecificationConfigRemove v-model="removeRef"></SpecificationConfigRemove>
    <SpecificationSubItemAdd v-model="subItemAddRef"></SpecificationSubItemAdd>
    <SpecificationSubItemRemove
      v-model="itemRemoveRef"
    ></SpecificationSubItemRemove>
  </div>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  getSpecificationConfigAllList,
  getSpecificationConfigPagination,
} from '@/api/fms-specification-config.api'
import { getCanUseCode } from '@/api/fms-code-dict.api'
import { Plus } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
// import { useUserPermission } from "@/utils/permission.utils";
import SpecificationConfigRemove from './SpecificationConfigRemove.vue'
import SpecificationConfigAddUpdate from './SpecificationConfigAddUpdate.vue'
import SpecificationSubItemAdd from './SpecificationSubItemAdd.vue'
import SpecificationSubItemRemove from './SpecificationSubItemRemove.vue'
import { statusOptions } from '@/utils/common.utils'
// const userPermission = useUserPermission();

/****
 * =============== 新增、修改相关操作 =====================
 */
const requestCanUseCode = (type: any, target: any) => {
  if (!type) {
    type = 2
  }

  getCanUseCode({ type })
    .then((res: any) => {
      console.log('getCanUseCode: ', res)
      target.form.number = res
    })
    .catch((err) => {
      ElMessage.error(err.msg)
    })
}

const addUpdateRef = reactive({
  visible: false,
  disabled: false,
  title: `规格配置`,
  curSelectRow: {},
  roleList: [],
  form: { enabled: 0 },
  rules: {},
  refreshDataList: () => {
    getDataList()
  },
})

const showAddEditDialog = (item: any, type: number) => {
  //修改子项
  if (item.specNumber) {
    subItemAddRef.visible = true
    subItemAddRef.specItem = { number: item.specNumber }
    subItemAddRef.specList = tableData.value
    subItemAddRef.form = item
    subItemAddRef.form.enabled = item.enabled + ''
    return
  }

  //新增 规格配置
  if (item.target) {
    let form = {
      id: null,
      enabled: '0',
      number: '',
      name: '',
    }
    addUpdateRef.visible = true
    addUpdateRef.disabled = false
    addUpdateRef.form = form
    requestCanUseCode(undefined, addUpdateRef)
    return
  }

  let form = {
    id: item.id,
    enabled: item.enabled + '',
    number: item.number,
    name: item.name,
  }
  addUpdateRef.visible = true
  addUpdateRef.disabled = true
  addUpdateRef.form = form
}

const subItemAddRef = reactive({
  visible: false,
  disabled: false,
  title: `规格子项`,
  curSelectRow: {},
  specItem: {},
  specList: [],
  form: {},
  rules: {},
  refreshDataList: () => {
    getDataList()
  },
})

const showAddSubItemDialog = (item: any, type: number) => {
  //新增 规格子配置
  subItemAddRef.visible = true
  subItemAddRef.specList = tableData.value
  subItemAddRef.form = {
    enabled: '0',
    number: '',
    name: '',
    specNumber: item.number,
  }
  requestCanUseCode(301, subItemAddRef)
}

/****
 * =============== 删除操作 =====================
 */
const removeRef = reactive({
  visible: false,
  title: '规格配置',
  curSelectRow: {},
  refreshDataList: () => {
    getDataList()
  },
})

const itemRemoveRef = reactive({
  visible: false,
  title: '规格配置',
  curSelectRow: {},
  refreshDataList: () => {
    getDataList()
  },
})

const showRemoveDialog = (item: any) => {
  if (item.specNumber) {
    itemRemoveRef.visible = true
    itemRemoveRef.curSelectRow = item
    return
  }

  removeRef.visible = true
  removeRef.curSelectRow = item
}

/****
 * =============== 搜索、表格、分页 =====================
 */
const formData = reactive({
  pageIndex: 1,
  pageSize: 10,
  total: 0,
})

const searchForm = ref({
  enabled: '',
  createTime: '',
  updateTime: '',
  createBy: '',
  updateBy: '',
  version: '',
  mcNo: '',
  number: '',
  name: '',
})

const tableLoading = ref(false)
const tableData = ref([])
/***
 * 开始搜索
 */
const startSearch = () => {
  formData.pageIndex = 1
  getDataList()
}

/***
 * 重置搜索条件
 */
const startClear = () => {
  formData.pageIndex = 1
  let value = searchForm.value
  Object.keys(value).forEach((key) => {
    ;(value as any)[key] = ''
  })
  getDataList()
}

const handleSizeChange = (value: number) => {
  formData.pageSize = value
  getDataList()
}

const handleCurrentChange = (value: number) => {
  formData.pageIndex = value
  getDataList()
}

const getDataList = () => {
  let searchCondition: any = {}

  let enabled = searchForm.value.enabled
  if (enabled) {
    searchCondition.enabled = enabled
  }
  let createTime = searchForm.value.createTime
  if (createTime) {
    searchCondition.createTime = createTime
  }
  let updateTime = searchForm.value.updateTime
  if (updateTime) {
    searchCondition.updateTime = updateTime
  }
  let createBy = searchForm.value.createBy
  if (createBy) {
    searchCondition.createBy = createBy
  }
  let updateBy = searchForm.value.updateBy
  if (updateBy) {
    searchCondition.updateBy = updateBy
  }
  let version = searchForm.value.version
  if (version) {
    searchCondition.version = version
  }
  let mcNo = searchForm.value.mcNo
  if (mcNo) {
    searchCondition.mcNo = mcNo
  }
  let number = searchForm.value.number
  if (number) {
    searchCondition.number = number
  }
  let name = searchForm.value.name
  if (name) {
    searchCondition.name = name
  }

  tableLoading.value = true
  getSpecificationConfigPagination({
    pageSize: formData.pageSize,
    pageIndex: formData.pageIndex,
    ...searchCondition,
  })
    .then(function (res: any) {
      tableLoading.value = false
      console.log(res)
      tableData.value = res.list || []
      formData.total = Number(res.total)
    })
    .catch((error) => {
      console.log(error)
      tableLoading.value = false
    })
}

onMounted(() => {
  getDataList()
})
</script>
<style lang="scss" scoped>
.specificationConfig-page {
  height: 100%;
}

.btns-fun-field {
  margin-bottom: 20px;
}

.pagination-field {
  display: flex;
  justify-content: flex-end;
  margin: 20px 0;
}

.btn-inner {
  display: flex;
  justify-content: center;
  color: #fff;
}

.tag-field {
  .el-tag {
    margin-right: 5px;
    &:last-child {
      margin-right: 0;
    }
  }
}

.search-field {
  margin-bottom: 12px;
  background: #fff;
  padding: 20px 20px 0px 20px;
  border-radius: 8px;
}

.specificationConfig-page-content {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
}
</style>
